"""
主逻辑交互模块
"""
from ATM.conf import settings
from ATM.core import log, account, transaction, auth

user_data={
    'account_id':None,
    'is_authenticated':False,
    'account_data':None
}

access_logger = log.log("access")
transaction_logger = log.log("transaction")

def account_info(access_data):
    print(access_data)

def repay(access_data):
    print(access_data)
    print("repay")
    account_data = account.load_account(access_data["account_id"])
    print(account_data)
    current_balance = '''
    ---------------------BALANCE INFO---------------------
    Credit:%s
    Balance:%s
    '''%(account_data["credit"],account_data["balance"])
    back_flag = False
    while not back_flag:
        print(current_balance)
        repay_amount = input("\033[31;1mInput repay amount(b=back):\033[0m").strip()
        if len(repay_amount) > 0 and repay_amount.isdigit():
            new_account_data = transaction.make_transaction(account_data,"repay",
                                                repay_amount, transaction_logger)
            if new_account_data:
                print("\033[42;1mNew Balance:%s\033[0m"%new_account_data["balance"])
        else:
            print("\033[31;1m%s is not valid amount, Only accept interge!\033[0m"%repay_amount)

        if repay_amount == 'b' or repay_amount == 'back':
            back_flag = True

def withdraw(access_data):
    print(access_data)
    print("withdraw")
    account_data = account.load_account(access_data["account_id"])
    print(account_data)
    current_balance = '''
    -------------------BALANCE INFO-------------------
    Credit:%s
    Balance:%s
    '''%(account_data["credit"],account_data["balance"])
    back_flag = False
    while not back_flag:
        print(current_balance)
        withdraw_amount = input("\033[31;1mInput withdraw amount(b=back):\033[0m").strip()
        if len(withdraw_amount) > 0 and withdraw_amount.isdigit():
            new_account_data = transaction.make_transaction(account_data,
                        "withdraw",withdraw_amount,transaction_logger)
            if new_account_data:
                print("\033[42;1mNew Balance:%s\033[0m"%new_account_data["balance"])
        else:
            print("\033[31;1m%s is not valid amount, Only accept interger!\033[0m"
                  %withdraw_amount)

        if withdraw_amount=='b' or withdraw_amount == 'back':
            back_flag = True

def transfer(access_data):
    print(access_data)
    print("transfer")
    account_data = account.load_account(access_data["account_id"])
    print(account_data)
    current_balance = '''
        ---------------------BALANCE INFO---------------------
        Credit:%s
        Balance:%s
        ''' % (account_data["credit"], account_data["balance"])
    back_flag = False
    while not back_flag:
        print(current_balance)
        transfer_amount = input("\033[31;1mInput transfer amount(b=back):\033[0m").strip()
        if len(transfer_amount) > 0 and transfer_amount.isdigit():
            new_account_data = transaction.make_transaction(account_data,"transfer",transfer_amount,transaction_logger)
            if new_account_data:
                print("\033[42;1mNew Balance:%s\033[0m" % new_account_data["balance"])
            new_account_data2 = transaction.make_transaction(account_data,"repay",
                                    new_account_data["balance"],transaction_logger)
            if new_account_data2:
                print("\033[42;1mNew Balance:%s\033[0m" % new_account_data2["balance"])
        else:
            print("\033[31;1m%s is not valid amount, Only accept interger!\033[0m"
                  % transfer_amount)
        if transfer_amount=='b' or transfer_amount == 'back':
            back_flag = True

def paycheck(access_data):
    time = input("pleas input time(Y-M-D):")
    log_file="%s/log/%s" % (settings.BASE_DIR, settings.LOGIN_TYPE["transaction"])
    print(log_file)
    with open(log_file,"r",encoding="utf-8") as f:
        for i in f.readlines():
            if time == i[0:10]:
                print(i)
            elif time == i[0:7]:
                print(i)
            elif time == i[0:4]:
                print(i)

def logout(access_data):
    q=input("If you want to quit, pleas input q:")
    if q == 'q':
        exit()

def interactive(access_data,** kewargs):
    msg = ('''
        ----------------chenlianxi Bank----------------
        \033[31;1m
        1.账户信息
        2.存款
        3.取款
        4.转账
        5.账单
        6.退出
        \033[0m
    ''')
    menu_dic={
        "1":account_info,
        "2":repay,
        "3":withdraw,
        "4":transfer,
        "5":paycheck,
        "6":logout,
    }
    flag = False
    while not flag:
        print(msg)
        choice = input("<<<:").strip()
        if choice in menu_dic:
            menu_dic[choice](access_data)
        else:
            print("\033[31;1mYou choice doesn't exit!\033[0m")

def run():
    access_data = auth.access_login(user_data, access_logger)
    print("AA")
    if user_data["is_authenticated"]:
        print("has authenticated")
        user_data["account_data"] = access_data
        interactive(user_data)